<template>
<div class="withdrawal">
    <section class="withdrawalmain">
        <section class="add">
            <h2 class="addtitle">{{$t('tibi[0]')}}</h2>
            <section class="fromadd">
                <el-form ref="form" :model="form" label-position="right" label-width="130px">
                    <el-form-item :label="$t('tibi[1]')">
                        <el-select v-model="form.wid" :placeholder="$t('tibi[2]')">
                            <el-option :label="item.code" :value="item.wid" v-for="(item,index) in rechargetype" :key="index"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item :label="$t('tibi[3]')">
                        <el-input v-model="form.address" :placeholder="$t('tibi[4]')"></el-input>
                    </el-form-item>
                    <el-form-item :label="$t('tibi[5]')">
                        <el-input v-model="form.notes" :placeholder="$t('tibi[6]')"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="handleSubmit">{{$t('tibi[7]')}}</el-button>
                    </el-form-item>
                </el-form>
            </section>
        </section>

        <section class="withdrawallist">
            <h2 class="listtitle">{{$t('tibi[8]')}}</h2>
            <ul class="title textcenter">
                <li class="list textleft">{{$t('tibi[9]')}}</li>
                <li class="list">{{$t('tibi[10]')}}</li>
                <li class="list">{{$t('tibi[11]')}}</li>
                <li class="list textright">{{$t('order[16]')}}</li>
            </ul>

            <template v-if="Addressdata">
                <ul class="tedlist textcenter" v-for="(item, index) in Addressdata" :key="index">
                    <li class="list textleft">{{item.address}}</li>
                    <li class="list">{{item.wid | getCode(rechargetype)}}</li>
                    <li class="list">{{item.notes}}</li>
                    <li class="list textright">
                        <el-button size="mini" type="primary" @click="deleteAddress(item)">{{$t('tibi[12]')}}</el-button>
                    </li>
                </ul>
            </template>

        </section>
    </section>
</div>
</template>

<script>
import {
    withdrawCoinList,
    createWithdrawAddress,
    getWithdrawAddress,
    deleteWithdrawAddress
} from "@/api/assetstotal";

// import { mapActions } from 'vuex'
// import { setToken } from "@/libs/util";
export default {
    name: "withdrawal",
    data() {
        return {
            form: {
                address: "",
                notes: "",
                wid: 1
            },
            types: "BTC",
            Addressdata: [],
            rechargetype: []
        };
    },
    filters: {
        getCode(wid, rechargetype) {
            for (let item of rechargetype) {
                if (wid == item.wid) {
                    return item.code
                }
            }
        }
    },
    methods: {
        typeChange(e) {
            // this.types = this.rechargetype[e].title;
            // let d= this.types.toLowerCase();
            // this.Addressdata = this.Addressdatas[d];
        },
        handleSubmit() {
            let _Data = JSON.parse(JSON.stringify(this.form))
            // _Data.type = this.rechargetype[_Data.type].wid;
            createWithdrawAddress(_Data)
                .then(res => {
                    this.$message({
                        message: this.$t("tibi[13]"),
                        type: "success"
                    });
                    this.form = {
                        address: "",
                        notes: "",
                        wid: 1
                    }
                    this.getdata();

                })
                .catch(err => {});
        },
        deleteAddress(row) {
            let _Data = {
                address_id: row.address_id
            };
            deleteWithdrawAddress(_Data)
                .then(res => {
                    if (res.data.status_code == 200) {
                        this.$message({
                            message: this.$t("tibi[14]"),
                            type: "success"
                        });
                        this.getdata();
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
                .catch(err => {});
        },
        getdata() {
            withdrawCoinList().then(res => {
                this.rechargetype = res;
                this.form.wid = res[0].wid;
            })
            getWithdrawAddress().then(res => {
                let list = res;
                let arr = []
                for (let item of list) {
                    arr = arr.concat(item.list)
                }
                this.Addressdata = arr
                console.log(this.Addressdata)
            })
        }
    },
    created() {
        this.getdata();
    }
};
</script>
